﻿Public Class Cart1
    Inherits System.Web.UI.Page

    Dim objFunc As New FunctionClass

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            bindData()
        End If
    End Sub

#Region "My Function"

    ' Load du lieu tu SESION cart
    Public Sub bindData()
        Try
            Dim objDT As System.Data.DataTable
            objDT = Session("Cart")
            gridCart.DataSource = objDT
            gridCart.DataBind()
            ' TInh tong gia tri don hang
            Dim total As Double = 0
            For Each objDR In objDT.Rows
                total += Double.Parse(objDR("Total").ToString)
            Next
            totalBill.InnerHtml = objFunc.formatVNCurrency(total.ToString) & " VND"
            Session("TotalBill") = total
        Catch ex As Exception

        End Try
    End Sub

    ' format tien viet
    Public Function formatVND(ByVal str As String) As String
        Return objFunc.formatVNCurrency(str)
    End Function

#End Region
    

#Region "Grid Cart"

    Private Sub gridCart_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gridCart.RowCancelingEdit
        gridCart.EditIndex = -1
        bindData()
    End Sub

    ' Tô màu cho từng dòng khi có sự kiện tạo row
    Private Sub gridCart_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridCart.RowCreated
        'Add CSS class on normal row.
        If e.Row.RowType = DataControlRowType.DataRow And e.Row.RowState = DataControlRowState.Normal Then
            e.Row.CssClass = "alt1"
        End If
        'Add CSS class on alternate row.
        If e.Row.RowType = DataControlRowType.DataRow And e.Row.RowState = DataControlRowState.Alternate Then
            e.Row.CssClass = "alt"
        End If
    End Sub

    ' Xử lý khi click nút Xóa
    Private Sub gridCart_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gridCart.RowDeleting
        Try
            Dim objDT As DataTable
            objDT = Session("Cart")
            objDT.Rows(e.RowIndex).Delete()
            ' Add lai session
            Session("Cart") = objDT
            ' thoat khoi su kien edit grid va Bind lai du lieu
            gridCart.EditIndex = -1
            bindData()
        Catch ex As Exception

        End Try
    End Sub

    ' Bắt đầu sự kiện Edit drid view
    Private Sub gridCart_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gridCart.RowEditing
        gridCart.EditIndex = e.NewEditIndex
        gridCart.RowStyle.CssClass = "colorEdit"
        bindData()
    End Sub

    ' Xử lý sự kiện update dữ liệu
    Private Sub gridCart_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gridCart.RowUpdating
        Try
            Dim lblId As Label = CType(gridCart.Rows(e.RowIndex).FindControl("lbl_ID"), Label)
            Dim txtQuantity As TextBox = CType(gridCart.Rows(e.RowIndex).FindControl("txtQuantity"), TextBox)
            Dim objDT As DataTable
            Dim objDR As DataRow
            objDT = Session("Cart")
            Dim total As Double = 0
            ' Chay tuong row trong table
            For Each objDR In objDT.Rows
                ' Neu co roi thi update so luong
                If objDR("IdCart") = lblId.Text Then
                    objDR("Quantity") = txtQuantity.Text
                    ' Lay lai bien so luong tai thoi diem hien tai de update tong gia tien
                    Dim currQuantity As Integer = Integer.Parse(objDR("Quantity").ToString)
                    Dim price As Double = Double.Parse(objDR("ProPrice").ToString)
                    total = price * currQuantity
                    objDR("Total") = total
                    Exit For
                End If
            Next
            ' Add lai session
            Session("Cart") = objDT
            ' thoat khoi su kien edit grid va Bind lai du lieu
            gridCart.EditIndex = -1
            bindData()
        Catch ex As Exception

        End Try
    End Sub

#End Region

End Class